package vip.liux.contracts.security.externalLogin.authenticator;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;

@Component
public class MockLoginAuthenticator implements ExternalLoginAuthenticator {
    @Override
    public UserDetails authenticate(String providerKey) {
        // 模拟的认证逻辑
        if (StringUtils.isNotBlank(providerKey) && StringUtils.startsWithIgnoreCase(providerKey, "mock")) {
            return new MockOidcUser(providerKey);
        }
        return null;
    }

    @Override
    public boolean supports(ExternalLoginProvider delimiter) {
        return delimiter.equals(ExternalLoginProvider.MOCK);
    }

    public static class MockOidcUser implements UserDetails {

        private final String username;

        public MockOidcUser(String username) {
            this.username = username;
        }

        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            return List.of();
        }

        @Override
        public String getPassword() {
            return "";
        }

        @Override
        public String getUsername() {
            return username;
        }
    }
}
